package org.jwcloud.project.module.zx.template.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.jwcloud.project.module.zx.template.service.RptTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.controller.CrudController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.PageListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;
import com.jw.mid.mm.comm.common.service.CrudService;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(value = "API - Controlle",
description = "模板信息", tags = "template")
@RestController
@RequestMapping("/rest/rptTemplate")
public class RptTemplateController extends BaseController{
/**
#督办、预警  自动刷数
show variables like 'event%';
SET GLOBAL event_scheduler = ON;

DROP EVENT event_cron_tip;

CREATE EVENT event_cron_tip
ON SCHEDULE
EVERY '1' MINUTE STARTS '2020-09-03 18:10:00'
DO call p_rpt_cron_tip();
 */
//	@Autowired
//    private CommonService commonService;
	
	@Autowired
	public RptCommonRepository rptCommonRepository;
	
	@Autowired
    private RptTemplateService rptTemplateService;
	
	@Autowired
    private RptUtService rptUtService;

    @GetMapping("queryCode_orgTypeList")
    @ApiOperation(value = "项目类型列表", notes = "项目类型列表")
    public Object queryCode_orgTypeList(
    		//@RequestBody(required = false) Map<String,String> param
    		){
    	return this.rptUtService.commonQeryCodeList_all("rta_code_org_type");
//        return this.quertCodeList("rta_code_org_type");
    }
    @GetMapping("queryCode_dutyList")
    @ApiOperation(value = "责任部门列表", notes = "责任部门列表")
    public Object queryCode_dutyList(
    		//@RequestBody(required = false) Map<String,String> param
    		){
    	return this.rptUtService.commonQeryCodeList("view_code_dety_org");
//        return this.quertCodeList("view_code_dety_org");
    }
    
    @GetMapping("queryTemplateList")
    @ApiOperation(value = "模板列表", notes = "主列表")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "template_name", value = "模板名", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "project_type", value = "项目类型", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "is_start", value = "启用", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "pagisize", value = "分页参数 currentPage pagisize...", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "currentPage", value = "分页参数 currentPage pagisize...", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "pagi", value = "分页参数 currentPage pagisize...", required = false, dataType = "string", paramType = "query"),
    })
    public Resp<Response<PageListSlice>> queryTemplateList(
    		Pagination pagination, 
    		@RequestParam(required = false) Map<String,String> param
    		){
//    	System.out.println(param);
//    	System.out.println(pagination.getPageSize());
//    	System.out.println("模板列表参数：" + param);
        return rptTemplateService.queryTemplateList(pagination, param);
    }
    
    @GetMapping("queryTemplateList_single")
    @ApiOperation(value = "模板列表 不分页", notes = "主列表")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "template_name", value = "模板名", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "project_type", value = "项目类型", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "is_start", value = "启用", required = false, dataType = "string", paramType = "query"),
    	
    })
    public Resp<List<Map<String, String>>> queryTemplateList(
    		@RequestParam(required = false) Map<String,String> param
    		){
        return rptTemplateService.queryTemplateList(param);
    }
    
    @PostMapping("doTemplateInsert")
    @ApiOperation(value = "添加模板", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "template_name", value = "模板名", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "project_type", value = "项目类型", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "content", value = "使用场景", required = true, dataType = "string", paramType = "query"),
    })
    public Resp<Object> doTemplateInsert(
    		@RequestBody(required = false) Map<String,String> param
    		){
    	
    	String flag = FmtUtil.nullRes(param
				, "template_name", "模板名"
				, "project_type", "项目类型"
				, "content", "使用场景");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
    	
        return rptTemplateService.doTemplateInsert(param);
    }
    
    @PostMapping("doTemplateUpdate")
    @ApiOperation(value = "修改模板", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "template_name", value = "模板名", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "project_type", value = "项目类型", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "content", value = "使用场景", required = true, dataType = "string", paramType = "query"),
    })
    public Resp<Object> doUpdate(
    		@RequestBody(required = false) Map<String,String> param
    		){
    	
    	String flag = FmtUtil.nullRes(param
    			, "id", "id"
				, "template_name", "模板名"
				, "project_type", "项目类型"
				, "content", "使用场景");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
    	
        return rptTemplateService.doTemplateUpdate(param);
    }
    @DeleteMapping("doTemplateDelete")
    @ApiOperation(value = "删除模板", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string", paramType = "query"),
    })
    public Resp<Object> doDelete(
    		@RequestParam(required = false) Map<String,String> param
    		){
    	
    	String flag = FmtUtil.nullRes(param
    			, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
    	
        return rptTemplateService.doTemplateDelete(param);
    }
    
    @DeleteMapping("doTemplateDelete_bat")
    @ApiOperation(value = "批量删除模板", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id 1;2;3;4", required = true, dataType = "string", paramType = "query"),
    })
    public Resp<Object> doTemplateDelete_bat(
    		@RequestParam(required = false) Map<String,String> param
    		){
    	
    	String flag = FmtUtil.nullRes(param
    			, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		String[] ids = param.get("id").split(";");
		Map map = new HashMap();
		for(String id: ids) {
			map.put("id", id);
			this.doDelete(map);
		}
    	
        return Resp.success(1);
    }
    
    @PostMapping("doTemplateSetStart")
    @ApiOperation(value = "设置模板启用", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "is_start", value = "1启用  0关闭", required = true, dataType = "string", paramType = "query"),
    })
    public Resp<Object> doTemplateSetStart(
    		@RequestBody(required = false) Map<String,String> param
    		){
    	
    	String flag = FmtUtil.nullRes(param
    			, "id", "id"
				, "is_start", "状态");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
    	
        return rptTemplateService.doTemplateSetStart(param);
    }
    
    @PostMapping("doTemplateCopy")
    @ApiOperation(value = "复制模板", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "要复制的模板id", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "template_name", value = "复制的模板名", required = true, dataType = "string", paramType = "query"),
    })
    public Resp<Object> doTemplateCopy(
    		@RequestBody(required = false) Map<String,String> param
    		){
    	
    	String flag = FmtUtil.nullRes(param
    			, "id", "id"
    			, "template_name", "复制的模板名");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
    	
        return rptTemplateService.doTemplateCopy(param);
    }
    
    
    @PostMapping("test")
    @ApiOperation(value = "后台测试用", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "模板名", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "name", value = "项目类型", required = false, dataType = "string", paramType = "query"),
    })
    public Resp<Object> test(
    		@RequestBody(required = false) Map<String,Object> param
    		){
    	
    	System.out.println("-------test-------");
    	System.out.println(param);
    	param.put("null1", "");
    	param.put("null2", null);
    	JSONObject.toJSON(null);
    	List li = new LinkedList();
    	li.add("ad1");
    	li.add("ad2");
    	li.add("ad3");
    	param.put("li", li);
    	
    	System.out.println(param.get("li_pre").getClass());
        return Resp.success(param);
    }
    
    
    //------------
	private List<Map<String, String>> quertCodeList(String tableName) {
		// TODO Auto-generated method stub
		return rptCommonRepository.queryCodeList(new HashMap() {{this.put("tableName", tableName);}});
	}
	
}